﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.xml;
using System.Xml;

public partial class Default2 : System.Web.UI.Page
{
    private DataSet _ds;
    protected void Page_Load(object sender, EventArgs e)
    {
      
        string filePath = @"C:\", // Path of the XML and PDF file needs  to be  stored
               msg = "New document has added", 
               hostName = "http://wss.u2ucourse.com/", 
               docLibrary= "Shared Document" //The document library into which pdf file needs  to be  updated
                          ;
        SPWeb spWeb = SPContext.Current.Web;
        SPListCollection col = spWeb.Lists;
    //    SPList spList = spWeb.Lists[listName];

        foreach (SPList spList in col)
        {
            _ds = new DataSet();
            try
            {
                DataTable dt = spList.Items.GetDataTable();
                if (dt != null)
                {
                    _ds.Tables.Add(dt);
                    _ds.AcceptChanges();
                }
                dt = null;

            }
            catch (Exception ex)
            {
                //To do :Error Handling
                Label2.Text = "Reading List" + ex.Message + "\n" + ex.Source + "\n" + ex.InnerException;
            }
            //XML creating from the data source
            if (_ds != null && _ds.Tables.Count > 0)
            {
                try
                {

                    _ds.WriteXml(filePath + spList.Title + ".xml");
                }
                catch (Exception ex)
                {
                    Label3.Text = "Writing xm" + ex.Message + "\n" + ex.Source + "\n" + ex.InnerException;
                }
            }


            //Accessing Data Table from the List
            //XML tarnsformation: Here XML is tarnsformed to represent in a  Table
            //Please refer ItextSharp DTD

            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(filePath + spList.Title + ".xml");
                XmlNode root = doc.DocumentElement;
                XmlNode sharePoint = doc.SelectSingleNode("NewDataSet");
                XmlNode MYNODE = doc.CreateNode("element", "itext", "");
                string innerXml = sharePoint.InnerXml;
                doc.ReplaceChild(MYNODE, sharePoint);
                XmlNode MYNODE1 = doc.CreateNode("element", "table", "");
              
              
                MYNODE.PrependChild(MYNODE1);
                innerXml = innerXml.Replace(spList.Title.Replace(" ", "_x0020_") + ">", "row>");
                innerXml = innerXml.Replace("<row>", "<row><cells>");
                innerXml = innerXml.Replace("</row>", "</cells></row>");
                MYNODE1.InnerXml = innerXml;
                doc.Save(filePath + spList.Title + ".xml");
                doc.Load(filePath + spList.Title + ".xml");
                int p = 0;
                XmlNodeList nl = doc.GetElementsByTagName("cells");
                foreach (XmlNode node in nl)
                {
                    XmlNodeList ls = node.ChildNodes;                    
                    XmlNode MYNODE11, row;

                    row = doc.CreateNode("element", "row", "");
                    if (p == 0)
                    {

                       // adding Header
                        MYNODE11 = doc.CreateNode("element", "cell", "");
                        MYNODE11.InnerXml = "Coulumn Name";
                        System.Xml.XmlAttribute hdr = doc.CreateAttribute("header");
                        hdr.Value = "true";
                        MYNODE11.Attributes.Append(hdr);
                        System.Xml.XmlAttribute bws = doc.CreateAttribute("borderwidth");
                        bws.Value = "0.0";
                        MYNODE11.Attributes.Append(bws);
                        row.AppendChild(MYNODE11);
                        node.ParentNode.ParentNode.PrependChild(row);
                        MYNODE11 = doc.CreateNode("element", "cell", "");
                        MYNODE11.InnerXml = "Value";
                        System.Xml.XmlAttribute hdrs = doc.CreateAttribute("header");
                        hdrs.Value = "true";
                        MYNODE11.Attributes.Append(hdrs);
                        System.Xml.XmlAttribute bws1 = doc.CreateAttribute("borderwidth");
                        bws1.Value = "0.0";
                        MYNODE11.Attributes.Append(bws1);                       
                        row.AppendChild(MYNODE11);
                        node.ParentNode.ParentNode.PrependChild(row);
                      //  adding decription
                        XmlNode dec_row = doc.CreateNode("element", "row", "");
                        XmlNode description = doc.CreateNode("element", "cell", "");
                        description.InnerXml = spList.Description + "Decription";
                        System.Xml.XmlAttribute colspan = doc.CreateAttribute("colspan");
                        colspan.Value = "2";
                        description.Attributes.Append(colspan);
                        System.Xml.XmlAttribute red = doc.CreateAttribute("red");
                        red.Value = "0";
                        description.Attributes.Append(red);
                        System.Xml.XmlAttribute green = doc.CreateAttribute("green");
                        green.Value = "0";
                        description.Attributes.Append(green);
                          System.Xml.XmlAttribute blue = doc.CreateAttribute("blue");
                        blue.Value = "255";
                        description.Attributes.Append(blue);
                        System.Xml.XmlAttribute borderWidth= doc.CreateAttribute("borderwidth");
                        borderWidth.Value = "0.0";
                        description.Attributes.Append(borderWidth);

                        dec_row.AppendChild(description);
                        node.ParentNode.ParentNode.PrependChild(dec_row);
                    }
                    foreach (XmlNode item in ls)
                    {

                        XmlNode rowNode = doc.CreateNode("element", "row", "");
                        XmlNode MYNODE12 = doc.CreateNode("element", "cell", "");
                        MYNODE12.InnerXml = item.Name;
                        MYNODE11 = doc.CreateNode("element", "cell", "");
                        MYNODE11.InnerXml = item.OuterXml;
                        System.Xml.XmlAttribute bws = doc.CreateAttribute("borderwidth");
                        bws.Value = "0.0";
                        MYNODE11.Attributes.Append(bws);
                         System.Xml.XmlAttribute bwsw = doc.CreateAttribute("borderwidth");
                         bwsw.Value = "0.0";
                        MYNODE12.Attributes.Append(bwsw);
                        rowNode.AppendChild(MYNODE12);
                        rowNode.AppendChild(MYNODE11);
                        
                        node.ParentNode.AppendChild(rowNode);
                        item.RemoveAll();
                    }
                    p++;
                    node.RemoveAll();
                }
                doc.Save(filePath + spList.Title + ".xml");
                doc.Load(filePath + spList.Title + ".xml");
                System.Xml.XmlAttribute attribute = doc.CreateAttribute("columns");
                attribute.Value = 2.ToString();
                XmlNode roots = doc.SelectSingleNode("itext");
                XmlNode table = roots.SelectSingleNode("table");
                table.Attributes.Append(attribute);
                System.Xml.XmlAttribute width1 = doc.CreateAttribute("width");
                width1.Value = "80%";
                table.Attributes.Append(width1);             

                System.Xml.XmlAttribute widths = doc.CreateAttribute("widths");
                widths.Value = "25;75";
                table.Attributes.Append(widths);
                System.Xml.XmlAttribute bw = doc.CreateAttribute("borderwidth");
                bw.Value = "0.0";
                table.Attributes.Append(bw);
                doc.Save(filePath + spList.Title + ".xml");               
            }
            catch (Exception ex)
            {
                Label3.Text = ex.Message + "\n"
                                                  + ex.Source + "\n"
                                                  + ex.StackTrace + "\n"
                                                  + ex.InnerException;
            }
            try
            {
                Document document = new Document(PageSize.A4);
                PdfWriter.GetInstance(document, new FileStream(filePath+spList.Title+".pdf", FileMode.Create));
                ITextHandler xmlHandler = new ITextHandler(document);
                xmlHandler.Parse(filePath + spList.Title + ".xml");



            }
            catch (Exception ex)
            {
                Label1.Text = ex.Message + "\n"
                                + ex.Source + "\n"
                                + ex.StackTrace + "\n"
                                + ex.InnerException;

            }

            try
            {
                SPWeb spWebs = SPContext.Current.Web;
                SPFolder docoLibFolder = spWebs.GetFolder(docLibrary);
                FileStream fStream = File.OpenRead(spList.Title + ".pdf");
                Byte[] Contents = new byte[Convert.ToInt32(fStream.Length)];
                fStream.Read(Contents, 0, Convert.ToInt32(fStream.Length));
                spWebs.AllowUnsafeUpdates = true;
                SPFile uploadedFile = docoLibFolder.Files.Add(hostName + spList.Title + ".pdf", Contents);
                Label1.Text = docoLibFolder.Name;
                // check the document in if it is checked out
                // (depends on the security settings of the document library)
                if (uploadedFile.CheckOutStatus != SPFile.SPCheckOutStatus.None)
                {
                    uploadedFile.CheckIn(msg);
                }
            }
            catch (Exception ex)
            {

                Label4.Text = ex.Message + "\n"
                               + ex.Source + "\n"
                               + ex.StackTrace + "\n"
                               + ex.InnerException;
            }

           
        }
    }
}
